home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / biblio / bibtex / utils / bibclean / vaxvms.c < prev    next >
C/C++ Source or Header  |  1992-10-12  |  24KB  |  851 lines

  1. /* -*-C-*- vaxvms.c */
  2. /*-->vaxvms*/
  3. /**********************************************************************/
  4. /****************************** vaxvms ********************************/
  5. /**********************************************************************/
  6.  
  7. /***********************************************************************
  8. This file provides alternative functions for several VMS VMS  C  library
  9. routines which either unacceptable, or incorrect, implementations.  They
  10. have  been developed and  tested under VMS Version  4.4, but indications
  11. are  that they apply  to  earlier versions, back to 3.2  at least.  They
  12. should be retested with each new release of VMS C.
  13.  
  14. Some of these (memxxx(), strxxx(),  and system()) are available with VMS
  15. C 2.3 or later, but these versions should still work.
  16.  
  17. Under VAX VMS 5.2, system(), sscanf(), and  strtod() are in the run-time
  18. library.  However, the  run-time  library version of system() returns  a
  19. VMS-style exit code, not a UNIX-style code; for portability, our version
  20. below is preferred.
  21.  
  22. Contents:
  23.     FSEEK
  24.     FTELL
  25.     GETCHAR
  26.     GETENV
  27.     READ
  28.     UNGETC
  29.     getjpi        -- system-service access
  30.     getlogin
  31.     memchr
  32.     memcmp
  33.     memcpy
  34.     memmove
  35.     memset
  36.     stricmp
  37.     strtok
  38.     strtol
  39.     system
  40.     tell
  41.     unlink
  42.     utime
  43.  
  44. The VAX VMS  file system record  structure has  unfortunate consequences
  45. for random access files.
  46.  
  47. By default, text files written  by most system  utilities, and languages
  48. other than  C, have a variable  length record format,  in which a 16-bit
  49. character count is  aligned  on an even-byte  boundary in the disk block
  50. b(always  512 bytes in  VMS, independent  of  record and  file formats),
  51. followed by <count> bytes  of data.  Binary  files,  such as .EXE, .OBJ,
  52. and  TeX .DVI and font files,  all use  a   512-byte fixed record format
  53. which has  no explicit  length field.  No  file  byte count  is  stored;
  54. instead, the block  count, and the offset of  the last data byte in  the
  55. last block are recorded in the  file header (do ``DUMP/HEADER filespec''
  56. to see it).  For binary files with  fixed-length records, the last block
  57. is  normally assumed to be  full,  and  consequently, file transfer   of
  58. binary data from other  machines via Kermit, FTP,  or DCL COPY from ANSI
  59. tapes, generally fails  because the input file  length is not a multiple
  60. of 512.
  61.  
  62. This record organization may  be contrasted with  the STREAM, STREAM_LF,
  63. and STREAM_CR organizations supported from Version 4.0; in  these,  disk
  64. blocks contain a continuous byte stream in which nothing, or  LF, or CR,
  65. is recognized as a record terminator.  These formats are similar to  the
  66. UNIX  and TOPS-20 file system  formats  which also use continuous   byte
  67. streams.
  68.  
  69. For C, this  means that a  program operating on a file  in record format
  70. cannot count input characters and expect that count to be the same value
  71. as the  offset parameter passed  to fseek(),  which  numerous C programs
  72. assume to  be the case.  The 15-Dec-1989 C  Standard,  and  Harbison and
  73. Steele's ``C Reference Manual'', emphasize that only  values returned by
  74. ftell() should be used as arguments to fseek(),  allowing the program to
  75. return to  a position previously read or  written.  UNFORTUNATELY, VMS C
  76. ftell()  DOES NOT  RETURN   A CORRECT  OFFSET VALUE FOR   RECORD  FILES.
  77. Instead, for record files, it returns the byte  offset  of the start  of
  78. the current record, no matter where in that  record the current position
  79. may  be.   This  misbehavior  is  completely unnecessary,   since    the
  80. replacements below perform correctly, and are written entirely in C.
  81.  
  82. Another problem is that ungetc(char  c, FILE  *fp) is unreliable.  VMS C
  83. implements  characters as  signed  8-bit integers (so do  many   other C
  84. implementations).  fgetc(FILE *fp)  returns an int,   not a char,  whose
  85. value is EOF (-1) in the event of end-of-file; however,  this value will
  86. also be  returned for   a character 0xFF,  so  it  is  essential  to use
  87. feof(FILE  *fp)  to test for  a true  end-of-file  condition when EOF is
  88. returned.  ungetc() checks  the sign  of its argument  c,  and if it  is
  89. negative (which it will be for 128 of the 256  signed bytes), REFUSES TO
  90. PUT IT BACK IN THE INPUT STREAM, on the assumption that c is really EOF.
  91. This  too can  be fixed;  ungetc()  should only  do  nothing  if  feof()
  92. indicates  a  true end-of-file   condition.   The overhead of  this   is
  93. trivial, since  feof() is  actually implemented as  a macro   which does
  94. nothing more than a logical AND and compare-with-zero.
  95.  
  96. getchar()  waits for a <CR> to  be typed when stdin is  a terminal;  the
  97. replacement vms_getchar() remedies this.
  98.  
  99. Undoubtedly  other  deficiencies  in   VMS  C will   reveal  themselves.
  100.  
  101. VMS read() returns   only  a  single  disk   block on  each call.    Its
  102. replacment, vms_read(), will  return  the  requested number of bytes, if
  103. possible.
  104.  
  105. [29-Apr-1987] Brendan Mackay (munnari!anucsd.oz!bdm@seismo.CSS.GOV)
  106. This fix has been incorporated in vms_read() below.  Here are  Brendan's
  107. comments:
  108. >> The code for vms_read() has problems.  One is that you don't test for
  109. >> end of file.  The other is that there is a bug in the C library which
  110. >> prevents you  asking for  more than  65535 bytes  at a  time.  It  is
  111. >> documented that no more  than 65535 bytes will  be returned, but  not
  112. >> that you can't ask for more.  If you do, it reduces your request  mod
  113. >> 65536!
  114.  
  115. There are  also a few  UNIX standard functions  which are unimplemented.
  116. getlogin() and unlink() have VMS  equivalents provided below.  tell() is
  117. considered obsolete, since its functionality is available  from lseek(),
  118. but it is still seen in a few programs, so is  provided below.  getenv()
  119. fails  if the name contains  a colon; its replacement  allows the colon,
  120. and ignores letter case.
  121.  
  122. In the interest  of  minimal source perturbation,  replacements  for VMS
  123. functions   are  given   the same  names,    but prefixed  "vms_".   For
  124. readability,   the original names  are  preserved,  but are converted to
  125. upper-case:
  126.  
  127.     #define FTELL vms_ftell
  128.     #define FSEEK vms_fseek
  129.     #define GETCHAR vms_getchar
  130.     #define GETENV vms_getenv
  131.     #define UNGETC vms_ungetc
  132.  
  133. These  are  only defined to work   correctly for fixed  length  512-byte
  134. records, and no check is made that the file has that organization (it is
  135. possible, but   not without  expensive calls to    fstat(), or access to
  136. internal library structures).
  137.  
  138. [02-Apr-1987]    -- Nelson H. F. Beebe, University of Utah Center for
  139.            Scientific Computing
  140. [13-Apr-1988]    -- added memxxx(), strxxx(), fixed return code in system()
  141. ***********************************************************************/
  142.  
  143. #ifdef VMS            /* so this compiles anywhere */
  144.  
  145. #if 1                /* prior to VMS C 2.3 */
  146. #define VOIDP    char*        /* char  *prior to Standard C */
  147. #define const            /* const is a type modifier in Standard C */
  148. #else  /* NOT 1 */
  149. #define VOIDP    void*        /* char  *prior to Standard C */
  150. #endif /* 1 */
  151.  
  152. #define FTELL    vms_ftell
  153. #define FSEEK    vms_fseek
  154. #define GETENV    vms_getenv
  155. #define GETCHAR vms_getchar
  156. #define READ    vms_read
  157. #define UNGETC    vms_ungetc
  158.  
  159. #include "os.h"
  160. #include <stdio.h>
  161. #include <stdlib.h>
  162. #include <types.h>
  163. #include <ctype.h>
  164. #include <descrip.h>
  165. #include <errno.h>        /* need for utime() */
  166. #include <iodef.h>        /* need for vms_getchar() */
  167. #include <rms.h>        /* need for utime() */
  168. #include <ssdef.h>
  169. #include <stat.h>
  170.  
  171. #if 0
  172. #include <string.h>             /* stupid VMS gets type of memchr() wrong! */
  173. #else  /* NOT 0 */
  174. char    *strchr();
  175. #endif /* 0 */
  176.  
  177. #include <time.h>        /* need for utime() */
  178.  
  179. static char rcsid[] = "$Id: vaxvms.c,v 1.7 1992/10/13 14:51:06 beebe Exp beebe $";
  180.  
  181. /* $Log: vaxvms.c,v $
  182.  * Revision 1.7  1992/10/13  14:51:06  beebe
  183.  * Correct implementation of memxxx() routines to use unsigned char
  184.  * pointers instead of void pointers.
  185.  * Include os.h to get definition of STDC symbol.
  186.  *
  187.  * Revision 1.6  1992/10/08  14:05:21  beebe
  188.  * Rename timeval variable to the_timeval to avoid shadowing.
  189.  *
  190.  * Revision 1.5  1992/10/08  14:01:22  beebe
  191.  * Add typecast (int) to fileno() invocations to work around incorrect types
  192.  * of stdio.h fileno() macros on some systems.
  193.  *
  194.  * Revision 1.4  1992/10/08  01:42:01  beebe
  195.  * Update for C++.
  196.  *
  197.  * Revision 1.3  1992/03/10  00:33:28  beebe
  198.  * *** empty log message ***
  199.  *
  200.  * Revision 1.2  1992/02/29  19:42:20  beebe
  201.  * Update for version 3.0.114 [29-Feb-1992] following two-month
  202.  * major overhaul and compilation testing on numerous machines.
  203.  *
  204.  * Revision 1.2  1992/02/29  19:42:20  beebe
  205.  * Update for version 3.0.114 [29-Feb-1992] following two-month
  206.  * major overhaul and compilation testing on numerous machines.
  207.  * */ 
  208.  
  209. /**********************************************************************/
  210. /*-->FSEEK*/
  211.  
  212. /* VMS fseek() and ftell() on fixed-length record files work correctly
  213. only at block boundaries.  This replacement code patches in the offset
  214. within    the  block.  Directions     from    current      position   and  from
  215. end-of-file are converted to absolute positions, and then the code for
  216. that case is invoked. */
  217.  
  218. long
  219. #if STDC
  220. FSEEK(
  221. FILE *fp,
  222. long n,
  223. long dir
  224. )
  225. #else /* NOT STDC */
  226. FSEEK(fp,n,dir)
  227. FILE *fp;
  228. long n;
  229. long dir;
  230. #endif /* STDC */
  231. {
  232.     long k,m,pos,val,oldpos;
  233.     struct stat buffer;
  234.  
  235.     for (;;)            /* loops only once or twice */
  236.     {
  237.       switch (dir)
  238.       {
  239.       case 0:            /* from BOF */
  240.       oldpos = FTELL(fp);    /* get current byte offset in file */
  241.       k = n & 511;        /* offset in 512-byte block */
  242.       m = n >> 9;        /* relative block number in file */
  243.       if (((*fp)->_cnt) && ((oldpos >> 9) == m)) /* still in same block */
  244.       {
  245.         val = 0;        /* success */
  246.         (*fp)->_ptr = ((*fp)->_base) + k;
  247.                     /* reset pointers to requested byte */
  248.         (*fp)->_cnt = 512 - k;
  249.       }
  250.       else
  251.       {
  252.         val = fseek(fp,m << 9,0);
  253.                 /* move to start of requested 512-byte block */
  254.         if (val == 0)    /* success */
  255.         {
  256.           (*fp)->_cnt = 0;    /* indicate empty buffer */
  257.           (void)fgetc(fp);    /* force refill of buffer */
  258.           (*fp)->_ptr = ((*fp)->_base) + k;
  259.                 /* reset pointers to requested byte */
  260.           (*fp)->_cnt = 512 - k;
  261.         }
  262.       }
  263.       return (val);
  264.  
  265.       case 1:            /* from current pos */
  266.       pos = FTELL(fp);
  267.       if (pos == EOF)    /* then error */
  268.         return (EOF);
  269.       n += pos;
  270.       dir = 0;
  271.       break;        /* go do case 0 */
  272.  
  273.       case 2:            /* from EOF */
  274.       val = fstat((int)fileno(fp),&buffer);
  275.       if (val == EOF)    /* then error */
  276.         return (EOF);
  277.       n += buffer.st_size - 1; /* convert filesize to offset and */
  278.                    /* add to requested offset */
  279.       dir = 0;
  280.       break;        /* go do case 0 */
  281.  
  282.       default:            /* illegal direction parameter */
  283.       return (EOF);
  284.       }
  285.     }
  286. }
  287.  
  288. /**********************************************************************/
  289. /*-->FTELL*/
  290.  
  291. /* With fixed-length record files, ftell() returns the offset of the
  292. start of block.     To get the true position, this must be biased by
  293. the offset within the block. */
  294.  
  295. long
  296. #if STDC
  297. FTELL(
  298. FILE *fp
  299. )
  300. #else /* NOT STDC */
  301. FTELL(fp)
  302. FILE *fp;
  303. #endif /* STDC */
  304. {
  305.     char c;
  306.     long pos;
  307.     long val;
  308.     if ((*fp)->_cnt == 0)    /* buffer empty--force refill */
  309.     {
  310.     c = fgetc(fp);
  311.     val = UNGETC(c,fp);
  312.     if (val != c)
  313.         return (EOF);    /* should never happen */
  314.     }
  315.     pos = ftell(fp);    /* this returns multiple of 512 (start of block) */
  316.     if (pos >= 0)        /* then success--patch in offset in block */
  317.       pos += ((*fp)->_ptr) - ((*fp)->_base);
  318.     return (pos);
  319. }
  320.  
  321. /**********************************************************************/
  322. /*-->GETCHAR*/
  323.  
  324. static int tt_channel = -1;    /* terminal channel for image QIO's */
  325.  
  326. #define FAILED(status) (~(status) & 1) /* failure if LSB is 0 */
  327.  
  328. int
  329. GETCHAR(VOID_ARG)
  330. {
  331.     int ret_char;        /* character returned */
  332.     int status;            /* system service status */
  333.     static $DESCRIPTOR(sys_in,"TT:");
  334.  
  335.     if (tt_channel == -1)    /* then first call--assign channel */
  336.     {
  337.     status = sys$assign(&sys_in,&tt_channel,0,0);
  338.     if (FAILED(status))
  339.         lib$stop(status);
  340.     }
  341.     ret_char = 0;
  342.     status = sys$qiow(0,tt_channel,IO$_TTYREADALL | IO$M_NOECHO,0,0,0,
  343.     &ret_char,1,0,0,0,0);
  344.     if (FAILED(status))
  345.     lib$stop(status);
  346.  
  347.     return (ret_char);
  348. }
  349.  
  350. /**********************************************************************/
  351. /*-->memchr*/
  352.  
  353. /* This is a simple implementation of memchr(), which searches for the
  354. first occurrence of a byte in the first n bytes of a byte string.  A
  355. library version should use hardware moves, or unrolled loops, or other
  356. tricks for greater efficiency. */
  357.  
  358. VOIDP
  359. #if STDC
  360. memchr(
  361. const VOIDP s,
  362. int c,
  363. size_t n
  364. )
  365. #else /* NOT STDC */
  366. memchr(s,c,n)
  367. const VOIDP s;
  368. int c;
  369. size_t n;
  370. #endif /* STDC */
  371. {
  372.     unsigned char *ss = (unsigned char*)s;
  373.  
  374.     for (; n > 0; ss++,--n)
  375.     {
  376.     if (*ss == (unsigned char)c)
  377.         return ((VOIDP)ss);
  378.     }
  379.     return ((VOIDP)NULL);
  380. }
  381.  
  382. /**********************************************************************/
  383. /*-->memcmp*/
  384.  
  385. /* This is a simple implementation of memcmp(), which compares two
  386. objects byte by byte, stopping after n bytes.  A library version
  387. should use hardware moves, or unrolled loops, or other tricks for
  388. greater efficiency. */
  389.  
  390. int
  391. #if STDC
  392. memcmp(
  393. const VOIDP s1,
  394. const VOIDP s2,
  395. size_t n
  396. )
  397. #else /* NOT STDC */
  398. memcmp(s1,s2,n)
  399. const VOIDP s1;
  400. const VOIDP s2;
  401. size_t n;
  402. #endif /* STDC */
  403. {
  404.     unsigned char *t1;
  405.     unsigned char *t2;    
  406.  
  407.     for (t1 = (unsigned char*)s1, t2 = (unsigned char*)s2; n > 0;
  408.         --n, t1++, t2++)
  409.     {
  410.         if (*t1 < *t2)
  411.             return (-(int)(t2 - (unsigned char*)s2));
  412.         else if (*t1 > *t2)
  413.             return ((int)(t2 - (unsigned char*)s2));
  414.     }
  415.     return (0);
  416. }
  417.  
  418. /**********************************************************************/
  419. /*-->memcpy*/
  420.  
  421. /* This is a simple implementation of memcpy(), which copies source
  422. to target with undefined behavior in the event of overlap.  This
  423. particular implementation copies from first to last byte, in order. */
  424.  
  425. VOIDP
  426. #if STDC
  427. memcpy(
  428. VOIDP t,    /* target */
  429. const VOIDP s,    /* source */
  430. size_t n
  431. )
  432. #else /* NOT STDC */
  433. memcpy(t,s,n)
  434. VOIDP t;    /* target */
  435. const VOIDP s;    /* source */
  436. size_t n;
  437. #endif /* STDC */
  438. {
  439.     unsigned char *ss = (unsigned char*)s;
  440.     unsigned char *tt = (unsigned char*)t;
  441.  
  442.     for (; n > 0; --n)
  443.     *tt++ = *ss++;            /* always copy in forward order */
  444. }
  445.  
  446. /**********************************************************************/
  447. /*-->memmove*/
  448.  
  449. /* This is a simple implementation of memmove(), which copies as if the
  450. source were first completely copied to a temporary area, then that
  451. area were copied to the target.    A library version should
  452. use hardware moves, or unrolled loops, or other tricks for greater
  453. efficiency. */
  454.  
  455. VOIDP
  456. #if STDC
  457. memmove(
  458. VOIDP t,    /* target */
  459. const VOIDP s,    /* source */
  460. size_t n
  461. )
  462. #else /* NOT STDC */
  463. memmove(t,s,n)
  464. VOIDP t;    /* target */
  465. const VOIDP s;    /* source */
  466. size_t n;
  467. #endif /* STDC */
  468. {
  469.     unsigned char *ss = (unsigned char*)s;
  470.     unsigned char *tt = (unsigned char*)t;
  471.  
  472.     if ((ss < tt) && ((ss + n) > tt))    /* source overlaps target from below */
  473.         for (ss += n, tt += n; n > 0; --n)
  474.             *tt-- = *ss--;    /* copy in reverse order */
  475.     else
  476.         for (; n > 0; --n)
  477.             *tt++ = *ss++;    /* copy in forward order */
  478. }
  479.  
  480. /**********************************************************************/
  481. /*-->memset*/
  482.  
  483. /* This is a simple implementation of memset().   A library version should
  484. use hardware moves, or unrolled loops, or other tricks for greater
  485. efficiency. */
  486.  
  487.  
  488. VOIDP
  489. #if STDC
  490. memset(
  491. VOIDP s,    /* target */
  492. int ch,        /* fill character (treated as unsigned char) */
  493. size_t n    /* fill count */
  494. )
  495. #else /* NOT STDC */
  496. memset(s,ch,n)
  497. VOIDP s;    /* target */
  498. int ch;        /* fill character (treated as unsigned char) */
  499. size_t n;    /* fill count */
  500. #endif /* STDC */
  501. {
  502.     unsigned char *ss = (unsigned char *)s;
  503.     
  504.     for (; n > 0; --n)
  505.         *ss++ = (unsigned char)ch;
  506. }
  507.  
  508. /**********************************************************************/
  509. /*-->READ*/
  510. int
  511. #if STDC
  512. READ(
  513. register int file_desc,
  514. register char *buffer,
  515. register int nbytes
  516. )
  517. #else /* NOT STDC */
  518. READ(file_desc,buffer,nbytes)
  519. register int file_desc;
  520. register char *buffer;
  521. register int nbytes;
  522. #endif /* STDC */
  523. {
  524.     register int ngot;
  525.     register int left;
  526.  
  527.     for (left = nbytes; left > 0; /* NOOP */)
  528.     {
  529.     ngot = read(file_desc,buffer,(left > 65024 ? 65024 : left));
  530.     if (ngot < 0)
  531.         return (-1);        /* error occurred */
  532.     if (ngot == 0)          /* eof occurred */
  533.         return (nbytes-left);
  534.     buffer += ngot;
  535.     left -= ngot;
  536.     }
  537.     return (nbytes-left);
  538. }
  539.  
  540. /**********************************************************************/
  541. /*-->UNGETC*/
  542. long
  543. #if STDC
  544. UNGETC(
  545. char c,
  546. FILE *fp
  547. )
  548. #else /* NOT STDC */
  549. UNGETC(c,fp) /* VMS ungetc() is a no-op if c < 0 (which is half the time!) */
  550. char c;
  551. FILE *fp;
  552. #endif /* STDC */
  553. {
  554.  
  555.     if ((c == EOF) && feof(fp))
  556.     return (EOF);        /* do nothing at true end-of-file */
  557.     else if ((*fp)->_cnt >= 512)/* buffer full--no fgetc() done in this block!*/
  558.     return (EOF);        /* must be user error if this happens */
  559.     else            /* put the character back in the buffer */
  560.     {
  561.       (*fp)->_cnt++;        /* increase count of characters left */
  562.       (*fp)->_ptr--;        /* backup pointer to next available char */
  563.       *((*fp)->_ptr) = c;    /* save the character */
  564.       return (c);        /* and return it */
  565.     }
  566. }
  567.  
  568. /**********************************************************************/
  569. /*-->getenv*/
  570. char*
  571. #if STDC
  572. GETENV(
  573. char  *name
  574. )
  575. #else /* NOT STDC */
  576. GETENV(name)
  577. char  *name;
  578. #endif /* STDC */
  579. {
  580.     char  *p;
  581.     char  *result;
  582.     char ucname[256];
  583.  
  584.     p = ucname;
  585.     while (*name)    /* VMS logical names must be upper-case */
  586.     {
  587.       *p++ = islower(*name) ? toupper(*name) : *name;
  588.       ++name;
  589.     }
  590.     *p = '\0';
  591.  
  592.     p = strchr(ucname,':');        /* colon in name? */
  593.     if (p == (char *)NULL)        /* no colon in name */
  594.     result = getenv(ucname);
  595.     else                /* try with and without colon */
  596.     {
  597.     result = getenv(ucname);
  598.     if (result == (char *)NULL)
  599.     {
  600.         *p = '\0';
  601.         result = getenv(ucname);
  602.         *p = ':';
  603.     }
  604.     }
  605.     return (result);
  606. }
  607.  
  608. /**********************************************************************/
  609. /*-->getjpi*/
  610.  
  611. /***********************************************************************
  612. Return  a system job/process value  obtained   from the VMS  system call
  613. LIB$GETJPI.   This call   can return  either 32-bit   integer values, or
  614. strings.  The  obtained value is stored in  an  internal  static  buffer
  615. which is overwritten on subsequent calls.
  616.  
  617. The function return is a (char*) pointer to  that buffer, which must  be
  618. coerced to  (long*) if an integer value is  obtained.  String values are
  619. guaranteed to be NUL terminated, with no trailing blanks.
  620.  
  621. The argument, jpi_code, is one of the values defined in <jpidef.h>.
  622.  
  623. In the event of an error return from LIB$GETJPI, (char*)NULL is returned
  624. instead.
  625.  
  626. [30-Oct-1987]
  627. ***********************************************************************/
  628.  
  629. #define LIB$_INVARG 0x158234        /* not defined in standard .h files */
  630.  
  631. int lib$getjpi();
  632.  
  633. char*
  634. #if STDC
  635. getjpi(
  636. int jpi_code                /* values defined in <jpidef.h> */
  637. )
  638. #else /* NOT STDC */
  639. getjpi(jpi_code)
  640. int jpi_code;                /* values defined in <jpidef.h> */
  641. #endif /* STDC */
  642. {
  643.     short retlen = 0;
  644.     long retval;
  645.     static char buffer[256];        /* space for up to 255-char results */
  646.     static $DESCRIPTOR(strdes,buffer);
  647.  
  648.     strdes.dsc$w_length = sizeof(buffer)-1; /* $DESCRIPTOR doesn't set this */
  649.  
  650.     /* lib$getjpi() will normally return a string representation.
  651.        Try first to get the integer representation, then if an invalid
  652.        argument is signalled, get the string representation. */
  653.  
  654.     retval = lib$getjpi(&jpi_code,0L,0L,&buffer[0]);
  655.     if (retval == LIB$_INVARG)
  656.     {
  657.     retval = lib$getjpi(&jpi_code,0L,0L,&buffer[0],&strdes,&retlen);
  658.     buffer[retlen] = '\0';        /* terminate any string value */
  659.     while ((retlen > 0) && (buffer[--retlen] == ' '))
  660.         buffer[retlen] = '\0';
  661.     }
  662.  
  663.     return ((retval == SS$_NORMAL) ? (char*)(&buffer[0]) : (char*)NULL);
  664. }
  665.  
  666. /**********************************************************************/
  667. /*-->getlogin*/
  668. char*
  669. getlogin(VOID_ARG)
  670. {
  671.     return ((char *)getenv("USER")); /* use equivalent VMS routine */
  672. }
  673.  
  674. /**********************************************************************/
  675. /*-->system*/
  676. int
  677. #if STDC
  678. system(
  679. char *s
  680. )
  681. #else /* NOT STDC */
  682. system(s)
  683. char *s;
  684. #endif /* STDC */
  685. {
  686.     struct    dsc$descriptor t;
  687.     int stat;
  688.  
  689.     t.dsc$w_length = strlen(s);
  690.     t.dsc$a_pointer = s;
  691.     t.dsc$b_class = DSC$K_CLASS_S;
  692.     t.dsc$b_dtype = DSC$K_DTYPE_T;
  693.  
  694.     /*******************************************************************
  695.     UNIX system() always returns 0 on success; interpretation of
  696.     non-zero returns varies with the particular implementation of UNIX,
  697.     but always means some kind of failure.  BSD UNIX returns 127 if the
  698.     shell, sh, cannot be executed, and otherwise returns 256*(program
  699.     exit code) + (wait() return value).
  700.  
  701.     The 3 low-order bits of stat return by LIB$SPAWN are:
  702.  
  703.     0    warning
  704.     1    success
  705.     2    error
  706.     3    information
  707.     4    severe or fatal error
  708.  
  709.     DCL returns 0 in the low-order bits for undefined commands, and CC
  710.     returns 0 for correctable syntax errors (it issues warnings for
  711.     them).
  712.  
  713.     We therefore consider values of 1 or 3 to be success.  LIB$SPAWN
  714.     will usually return SS$_NORMAL, independent of the value of stat,
  715.     but if it fails, we follow BSD UNIX and return 127.
  716.     *******************************************************************/
  717.  
  718.     if (LIB$SPAWN(&t,0,0,0,0,0,&stat) != SS$_NORMAL)
  719.     return (127);
  720.     switch (stat & 7)
  721.     {
  722.     case 0:
  723.     return (256);
  724.     case 1:
  725.     case 3:
  726.     return (0);
  727.     default:
  728.     return (stat << 8);
  729.     }
  730. }
  731.  
  732. /**********************************************************************/
  733. /*-->tell*/
  734. long
  735. #if STDC
  736. tell(
  737. int handle
  738. )
  739. #else /* NOT STDC */
  740. tell(handle)
  741. int handle;
  742. #endif /* STDC */
  743. {
  744.     return (lseek(handle,0L,1));
  745. }
  746.  
  747. /**********************************************************************/
  748. /*-->unlink*/
  749. int
  750. #if STDC
  751. unlink(
  752. char *filename
  753. )
  754. #else /* NOT STDC */
  755. unlink(filename)
  756. char *filename;
  757. #endif /* STDC */
  758. {
  759.     return (delete(filename)); /* use equivalent VMS routine */
  760. }
  761.  
  762. /**********************************************************************/
  763. /*-->utime*/
  764.  
  765. /* utime(path,times) sets the access and modification times of the
  766.    file 'path' to the UNIX binary time values, 'times'.  Return 0
  767.    on success, and -1 on error (setting errno as well). */
  768.  
  769. #if STDC
  770. utime(
  771. char  *path,
  772. time_t times[2]
  773. )
  774. #else /* NOT STDC */
  775. utime(path,times)        /* VAX VMS C version */
  776. char  *path;
  777. time_t times[2];
  778. #endif /* STDC */
  779. {
  780.     int status;
  781.     struct dsc$descriptor_s time_desc;
  782.     char *ftime = "23-OCT-1907 12:34:56";
  783.     struct tm *the_timeval;
  784.     static char  *months[] = {"JAN","FEB","MAR","APR","MAY","JUN",
  785.                  "JUL","AUG","SEP","OCT","NOV","DEC"};
  786.     struct FAB fab1;
  787.     struct XABRDT xab1;
  788.  
  789.     /* Zero FAB and XAB structures */
  790.     (void)memset(&fab1,'\0',sizeof(fab1));
  791.     (void)memset(&xab1,'\0',sizeof(xab1));
  792.  
  793.     /* Convert UNIX binary time to ASCII string for
  794.        sys$bintime().  We use localtime() instead of ctime(),
  795.        because although ctime() is simpler, it drops the seconds
  796.        field, which we would rather preserve.  */
  797.  
  798.     the_timeval = (struct tm*)localtime(×[0]);
  799.     sprintf(ftime,"%02d-%3s-19%02d %02d:%02d:%02d",
  800.     the_timeval->tm_mday,
  801.     months[the_timeval->tm_mon],
  802.     the_timeval->tm_year,
  803.     the_timeval->tm_hour,
  804.     the_timeval->tm_min,
  805.     the_timeval->tm_sec);
  806.  
  807.     /* Setup fab1 and rab fields. */
  808.     fab1.fab$b_bid = FAB$C_BID;
  809.     fab1.fab$b_bln = FAB$C_BLN;
  810.     fab1.fab$l_fop = FAB$V_UFO;
  811.     fab1.fab$b_fac = FAB$V_GET;
  812.     fab1.fab$l_fna = path;
  813.     fab1.fab$b_fns = strlen(path);
  814.     fab1.fab$l_xab = (char*)&xab1;
  815.  
  816.     xab1.xab$b_bln = XAB$C_RDTLEN;
  817.     xab1.xab$b_cod = XAB$C_RDT;
  818.     xab1.xab$l_nxt = (char*)NULL;
  819.  
  820.     /* Open the file */
  821.     status = sys$open(&fab1);
  822.     if (status != RMS$_NORMAL)
  823.     {
  824.       errno = ENOENT;
  825.       return (-1);
  826.     }
  827.  
  828.     /* Convert the time string to a VMS binary time value in the XAB */
  829.     time_desc.dsc$w_length = strlen(ftime);
  830.     time_desc.dsc$a_pointer = ftime;
  831.     time_desc.dsc$b_class = DSC$K_CLASS_S;
  832.     time_desc.dsc$b_dtype = DSC$K_DTYPE_T;
  833.     status = sys$bintim(&time_desc,&xab1.xab$q_rdt);
  834.     if (status != SS$_NORMAL)
  835.     {
  836.       status = sys$close(&fab1);
  837.       errno = EFAULT;
  838.       return (-1);
  839.     }
  840.  
  841.     /* Close the file, updating the revision date/time value */
  842.     status = sys$close(&fab1);
  843.     if (status != RMS$_NORMAL)
  844.     {
  845.       errno = EACCES;
  846.       return (-1);
  847.     }
  848.     return (0);
  849. }
  850. #endif /* VMS */
  851.